package com.hjt.db;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

import com.hjt.db.DBManager.RequestParam;
import com.hjt.utils.FileUtil;

import lombok.Data;

public class DBManager {	
	
	@Data
	public static class RequestParam {
		private byte[] imgData;
		private String path;
		private String name;
		private Double pointX;
		private Double pointY;
		private String deviceId;
	}
	
	
	private static Connection getCon() {
		String url = "jdbc:mysql://127.0.0.1:3306/game_help?useUnicode=true&characterEncoding=utf8";
		String username = "root";
		String password = "123456";
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			return DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void createRecord(RequestParam param) {
		Connection con = getCon();
		PreparedStatement psp = null;
		try {
			psp = con.prepareStatement("INSERT INTO `t_game_screen_record` (`device_id`,`x`,`y`,`screen_snapshot`,`create_time`) VALUES(?,?,?,?,?)");
			psp.setObject(1, param.getDeviceId());
			psp.setObject(2, param.getPointX());
			psp.setObject(3, param.getPointY());
			psp.setObject(4, param.getPath());
			psp.setObject(5, new Date());
			int rs = psp.executeUpdate();
			System.out.println("写入结果：" + rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				psp.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		File files = new File("D:\\Game Helper\\logs\\log");
		File f = files.listFiles()[0];
		RequestParam req = new RequestParam();
		req.setDeviceId("111");
		req.setImgData(FileUtil.readBytesFromFile(f));
		req.setName(f.getName());
		req.setPointX(1d);
		req.setPointY(2d);
		req.setPath(f.getAbsolutePath());
		createRecord(req);
	}
}
